package io.simple.sessions;

import java.util.Collection;
import java.util.Map;
import java.util.NavigableSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListSet;

/**
 * Created by rehak on 2/25/14.
 */
public class LocalSession implements Session, ConcurrentMap<String, Object> {

    private final String id;
    private final long createdOn;
//    private final NavigableSet<Long> refreshes = new ConcurrentSkipListSet<>();
    private final ConcurrentMap<String, Object> props = new ConcurrentHashMap<>();

    public LocalSession(String id, long createdOn) {
        this.id = id;
        this.createdOn = createdOn;
    }

    public long getCreatedOn() {
        return createdOn;
    }

    public void refresh(long ts) {
        //refreshes.add(ts);
    }

    @Override
    public String getID() {
        return id;
    }

    @Override
    public boolean isValid() {
        return true;
    }

    // delegated

    @Override
    public Object putIfAbsent(String key, Object value) {
        return props.put(key, value);
    }

    @Override
    public boolean remove(Object key, Object value) {
        return props.remove(key, value);
    }

    @Override
    public boolean replace(String key, Object oldValue, Object newValue) {
        return props.replace(key, oldValue, newValue);
    }

    @Override
    public Object replace(String key, Object value) {
        return props.replace(key, value);
    }

    @Override
    public int size() {
        return props.size();
    }

    @Override
    public boolean isEmpty() {
        return props.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return props.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return props.containsValue(value);
    }

    @Override
    public Object get(Object key) {
        return props.get(key);
    }

    @Override
    public Object put(String key, Object value) {
        return props.put(key, value);
    }

    @Override
    public Object remove(Object key) {
        return props.remove(key);
    }

    @Override
    public void putAll(Map m) {
        props.putAll(m);
    }

    @Override
    public void clear() {
        props.clear();
    }

    @Override
    public Set<String> keySet() {
        return props.keySet();
    }

    @Override
    public Collection<Object> values() {
        return props.values();
    }

    @Override
    public Set<Entry<String, Object>> entrySet() {
        return props.entrySet();
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("LocalSession{");
        sb.append("id='").append(id).append('\'');
        sb.append(", createdOn=").append(createdOn);
   //     sb.append(", refreshes=").append(refreshes);
        sb.append(", props=").append(props);
        sb.append('}');
        return sb.toString();
    }
}
